#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#define High 18
#define Width 30
int score;  //分数
char c; //判断输入
int pos_x, pos_y; // 板子位置
int ball_x, ball_y; //球的位置
int vx, vy; // 小球速度
int speed;
int speedx; //砖块速度
int b[10];    // 定义砖块

int i, j; // 循环变量
int k[100][100];






void gotoxy(int x,int y)  //光标移动到(x,y)位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void HideCursor() // 用于隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};  // 第二个值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}


void start (){
	score = 0;
	pos_x = High;
	pos_y = Width / 2;
	ball_x = pos_x - 1;
	ball_y = pos_y;
	for (i = 1; i <= High; i ++){
		for (j = 1; j <= Width; j ++){
			k[i][j] = 0;
		}
	}
	for (i = 1; i <= High; i ++){         //定义边界
		k[i][1] = 2;
		k[i][Width] = 2;
	}
	vx = -1;
	vy = -1;
	speed = 0;
	speedx = 0;

	for (i = 2; i < Width; i ++){
		k[1][i] = 5;
	}



	HideCursor(); // 隐藏光标

}


void user (){
	if(kbhit()){  // 判断是否有输入
		c = getch();

		
		if (c == 'a'){
			pos_y --;
			k[pos_x][pos_y + 3] = 0;
		}

		else if (c == 'd'){
			pos_y ++;
			k[pos_x][pos_y - 3] = 0;
		}
		else if (c == 27){
			getch();
		}

	}
}


void show (){
	gotoxy(0,0);  // 光标移动到原点位置，以下重画清屏
	for (i = 1; i <= High; i ++){
		for (j = 1; j <= Width; j ++){
			if (k[i][j] == 1)
				printf("~");
			else if (k[i][j] == 2)
				printf("|");
			else if (k[i][j] == 9)
				printf("o");
			else if (k[i][j] == 5)
				printf("B");
			else if (k[i][j] == 4)
				printf("B");

			
			
			






			else
				printf (" ");
			
		}
		printf("\n");
		
	}
	printf ("  分数：%d", score); 
	
	
}




void update (){
	speed ++;
	speedx ++;


	if (pos_y - 3 < 1)             // 防止挡板出界
		pos_y ++;
	if (pos_y + 3 > Width)
		pos_y --;



	for (i = 1; i <= 5; i ++){      // 更新挡板位置
		k[pos_x][pos_y - 3 + i] = 1;
	}




	if(speed == 10){
		speed = 0;

		if (k[ball_x + vx][ball_y + vy] == 5){     //击中砖块
			k[ball_x - vx][ball_y - vy] = 0;
			k[ball_x + vx][ball_y] = 0;   //消掉砖块
			vx = -vx;
			score ++;
		}


		ball_x = ball_x + vx;
		ball_y = ball_y + vy;
		k[ball_x][ball_y] = 9;              //更新球的位置
		k[ball_x - vx][ball_y - vy] = 0;

		if (ball_y == 2 || ball_y == Width - 1)     //碰壁反弹
			vy = -vy;
		if (ball_x == 1)
			vx = -vx;
		if (ball_x > High){
			printf ("\nGameOver\n按任意键结束游戏\n");       //球掉地 游戏结束
			getch();
			exit(0);
		}

		if (ball_x + 1 == pos_x && abs (ball_y - pos_y) <= 3)   // 档板反弹
			vx = -vx;
	}
	if (speedx == 200){                          //砖块下落

		for (i = High; i >= 1; i --){
			for (j = 1; j < Width; j ++){
				if (k[i][j] == 5){
					k[i][j] = 0;
					if (k[i + 1][j] == 1){
						printf ("\nGameOver\n按任意键结束游戏\n");       //砖块砸到板子 游戏结束
						getch();
						exit(0);
					}

					k[i + 1][j] = 5;
				}
			}
		}
		for (i = 2; i < Width; i ++){
			k[1][i] = 5;
		}

		speedx = 0;
		
	}

}





int main(){
    start();
	while(1){
		show();
		user();
		update();

	}
	return 0;
}